from django.shortcuts import render
from rest_framework.views import APIView
# Create your views here.
from cake import models,ser
from rest_framework.response import Response
from rest_framework.filters import SearchFilter
from rest_framework.viewsets import ModelViewSet
# 轮播图
class BannerView(APIView):
    def get(self, request):
        data = models.BannerModel.objects.all()
        Ser = ser.BannerSerializer(instance=data, many=True)
        return Response(Ser.data)

# 首页精品推荐
class CakeJPView(APIView):
    def get(self, request):
        data = models.CakeModel.objects.all()[0:3]
        Ser = ser.CakeSerializer(instance=data, many=True)
        return Response(Ser.data)
# 首页蛋糕分类
class CakeCateView(APIView):
    def get(self, request):
        id=request.GET.get('id')
        data = models.CakeModel.objects.filter(cate_id=id)[0:3]
        Ser = ser.CakeSerializer(instance=data, many=True)
        return Response(Ser.data)

class Category(APIView):
    def get(self,request):
        id=request.GET.get('id')
        data = models.CategoryModel.objects.filter(cate=id)
        Ser = ser.Category(instance=data, many=True)
        return Response(Ser.data)

class CakeView(APIView):
    def get(self, request):
        id=request.GET.get('id')
        try:
            cate = request.GET.get('cate')
            if cate is None:
                data = models.CakeModel.objects.filter(cate=id)
                Ser = ser.CakeSerializer(instance=data, many=True)
                return Response(Ser.data)
            else:
                data = models.CakeModel.objects.filter(category=cate)
                Ser = ser.CakeSerializer(instance=data, many=True)
                return Response(Ser.data)
        except:
            data = models.CakeModel.objects.filter(cate=id)
            Ser = ser.CakeSerializer(instance=data, many=True)
            return Response(Ser.data)

class BreadView(APIView):
    def get(self, request):
        id=request.GET.get('id')
        data = models.CakeModel.objects.filter(cate=id)
        Ser = ser.CakeSerializer(instance=data, many=True)
        return Response(Ser.data)

# 首页蛋糕卷分类
class RollsView(APIView):
    def get(self, request):
        id=request.GET.get('id')
        data = models.CakeModel.objects.filter(cate=id)[0:3]
        Ser = ser.CakeSerializer(instance=data, many=True)
        return Response(Ser.data)

# 模糊搜索
class SelectView(ModelViewSet):
    queryset = models.CakeModel.objects.all()
    serializer_class = ser.CakeSerializer
    lookup_field = "id"
    lookup_url_kwarg = "id"
    filter_backends = [SearchFilter]
    search_fields = ['title','tro','price']

class DemilView(APIView):
    def get(self, request):
        id=int(request.GET.get('id'))
        data = models.CakeDemil.objects.filter(cake_id=id)
        Ser = ser.CakeDemilSer(instance=data, many=True)
        return Response(Ser.data)

class CategoryView(APIView):
    def get(self, request):
        id=request.GET.get('id')
        data = models.CategoryModel.objects.filter(category=id)
        Ser = ser.Category(instance=data, many=True)
        return Response(Ser.data)

# 弹窗
class CakeDetailView(APIView):
    def post(self, request):
        id=request.data.get('id')
        data = models.CakeModel.objects.filter(id=id)
        Ser = ser.CakeSerializer(instance=data,many=True)
        return Response(Ser.data)